import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SCrossBeachOpen } from "../../../Message/Public/SCrossBeachOpen";
import { SCrossBeachList } from "../../../Message/Public/SCrossBeachList";
import { SCrossBeachInfo } from "../../../Message/Public/SCrossBeachInfo";
import { CrossBeachEnterData } from "../view/crossBeach/CrossBeachEnterData";
import { GameConst } from "../../component/gconst/GameConst";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { CrossBeachIconView } from "../view/crossBeach/CrossBeachIconView";
import { CrossBeachEnterWindow } from "../view/crossBeach/CrossBeachEnterWindow";
import { CrossBeachBtnView } from "../view/crossBeach/CrossBeachBtnView";
import { CrossBeachData } from "../view/crossBeach/CrossBeachData";
import { GameController } from "../mvc/GameController";
import { ClockManager } from "../manager/ClockManager";
type int = number;
//class CrossBeachController
    
    export  class CrossBeachController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.CrossBeachOpenCommand, this.onCrossBeachOpenCommand);
            NetDispatcher.addCmdListener(ServerCommand.CrossBeachCloseCommand, this.onCrossBeachCloseCommand);
            NetDispatcher.addCmdListener(ServerCommand.CrossBeachListCommand, this.onCrossBeachListCommand);
            NetDispatcher.addCmdListener(ServerCommand.ActiveSunBathPass, this.onActiveSunBathPassHandler);
            Dispatcher.addEventListener(EventName.CrossBeachShowEnterWindow, this.onCrossBeachShowEnterWindow);
            Dispatcher.addEventListener(EventName.CrossBeachRequestList, this.onCrossBeachRequestList);
            Dispatcher.addEventListener(EventName.CrossBeachRequestEnter, this.onCrossBeachRequestEnter);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.SunBathPass, this.onSunBathPassHandler);
            Dispatcher.addEventListener(EventName.SunBathPassSuccess, this.onSunBathPassSuccessHandler, false, 1000);
            Dispatcher.addEventListener(EventName.SunBathMakeUpSuccess, this.onSunBathMakeUpSuccess, false, 1000);
            return;
        }

        /* internal  */onCrossBeachOpenCommand(arg1: SCrossBeachOpen): void
        {
            this._isInCrossBeachActive = true;
            this.crossBeachIconView.show();
            return;
        }

        /* internal  */onCrossBeachCloseCommand(arg1: Object): void
        {
            this._isInCrossBeachActive = false;
            if (this._crossBeachIconView) 
            {
                this.crossBeachIconView.hide();
            }
            if (this._crossBeachEnterWindow) 
            {
                this.crossBeachEnterWindow.hide();
            }
            return;
        }

        /* internal  */onCrossBeachListCommand(arg1: SCrossBeachList): void
        {
            var loc3=null;
            var loc4=null;
            var loc1=new Array<any>();
            var loc2=0;
            while (loc2 < arg1.crossBeaches.length) 
            {
                loc3 = arg1.crossBeaches[loc2] as SCrossBeachInfo;
                (loc4 = new CrossBeachEnterData()).index = loc2 + 1;
                loc4.maxPlayerNum = GameConst.CrossBeachMaxPlayerNum;
                loc4.playerNum = loc3.playerNum;
                loc4.corssBeachId = loc3.beachId;
                loc1.push(loc4);
                ++loc2;
            }
            this.crossBeachEnterWindow.updateList(loc1);
            return;
        }

        /* internal  */onActiveSunBathPassHandler(arg1: SActiveToClient): void
        {
            if (this._isInCrossBeachActive && this._crossBeachIconView && !this._crossBeachIconView.isHide) 
            {
                this._crossBeachIconView.updateActiveToClientInfo(arg1);
            }
            return;
        }

        /* internal  */onCrossBeachShowEnterWindow(arg1: DataEvent): void
        {
            this.crossBeachEnterWindow.show();
            return;
        }

        /* internal  */onCrossBeachRequestList(arg1: DataEvent): void
        {
            GameProxy.crossBeach.getCrossBeachList();
            return;
        }

        /* internal  */onCrossBeachRequestEnter(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            GameProxy.crossBeach.enterCrossBeach(loc1);
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (GameMapUtil.curMapState.isCrossBeach) 
            {
                if (!this._isInCrossBeach) 
                {
                    this._isInCrossBeach = true;
                    if (this._crossBeachIconView) 
                    {
                        this.crossBeachIconView.hide();
                    }
                    if (this._crossBeachEnterWindow) 
                    {
                        this.crossBeachEnterWindow.hide();
                    }
                }
            }
            else if (this._isInCrossBeach) 
            {
                this._isInCrossBeach = false;
                if (this._isInCrossBeachActive) 
                {
                    if (this._crossBeachIconView) 
                    {
                        this.crossBeachIconView.show();
                    }
                }
            }
            return;
        }

        /* internal  */get crossBeachIconView(): CrossBeachIconView
        {
            if (!this._crossBeachIconView) 
            {
                this._crossBeachIconView = new CrossBeachIconView();
            }
            return this._crossBeachIconView;
        }

        /* internal  */get crossBeachEnterWindow(): CrossBeachEnterWindow
        {
            if (!this._crossBeachEnterWindow) 
            {
                this._crossBeachEnterWindow = new CrossBeachEnterWindow();
            }
            return this._crossBeachEnterWindow;
        }

        /* internal  */get crossBeachBtnView(): CrossBeachBtnView
        {
            if (!this._crossBeachBtnView) 
            {
                this._crossBeachBtnView = new CrossBeachBtnView();
            }
            return this._crossBeachBtnView;
        }

        public get isInCrossBeachActive(): Boolean
        {
            return this._isInCrossBeachActive;
        }

        /* internal  */onSunBathPassHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.sunBathPass(loc1);
            return;
        }

        /* internal  */onSunBathPassSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Date;
            CrossBeachData.sunBathPassCdEndDate = loc1;
            GameController.dialog.view.hide();
            return;
        }

        /* internal  */onSunBathMakeUpSuccess(arg1: DataEvent): void
        {
            var loc1=new Date(ClockManager.instance.nowDate.time + 10000);
            CrossBeachData.sunBathMakeUpCdEndDate = loc1;
            GameController.dialog.view.hide();
            return;
        }

        private /* var */_isInCrossBeachActive: Boolean=false;

        private /* var */_isInCrossBeach: Boolean=false;

        private /* var */_crossBeachIconView: CrossBeachIconView;

        private /* var */_crossBeachEnterWindow: CrossBeachEnterWindow;

        private /* var */_crossBeachBtnView: CrossBeachBtnView;
    }
